package com.ltcode.auth.interfaces.controller;

import com.ltcode.auth.application.dto.LoginCommand;
import com.ltcode.auth.application.dto.LoginResult;
import com.ltcode.auth.application.dto.RegisterCommand;
import com.ltcode.auth.application.service.AuthApplicationService;
import com.ltcode.auth.domain.model.AuthUser;
import com.ltcode.auth.interfaces.request.LoginRequestDTO;
import com.ltcode.auth.interfaces.response.LoginResponseDTO;
import com.ltcode.auth.interfaces.request.RegisterRequestDTO;
import com.ltcode.auth.interfaces.converter.AuthDTOConverter;
import com.ltcode.common.base.web.domain.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 认证控制器
 *
 * @author ltcode
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping
@RequiredArgsConstructor
public class AuthController {

    private final AuthApplicationService authApplicationService;

    @PostMapping("/login")
    public ApiResponse<LoginResponseDTO> login(@Validated @RequestBody LoginRequestDTO request) {
        // 转换为应用层命令
        LoginCommand command = AuthDTOConverter.toLoginCommand(request);
        // 执行登录
        LoginResult result = authApplicationService.login(command);
        // 转换为响应DTO
        LoginResponseDTO response = AuthDTOConverter.toLoginResponseDTO(result);

        return ApiResponse.ok(response);
    }

    //    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ApiResponse<Void> register(@RequestBody RegisterRequestDTO request) {
        // 转换为应用层命令
        RegisterCommand command = AuthDTOConverter.toRegisterCommand(request);
        command.validatePasswordConsistency();
        // 执行注册
        authApplicationService.register(command);
        return ApiResponse.ok();
    }

    @PostMapping("/logout")
    public ApiResponse<Void> logout(HttpServletRequest request) {
        String accessToken = getAccessTokenFromRequest(request);
        if (accessToken != null) {
            authApplicationService.logout(accessToken);
        }
        return ApiResponse.ok();
    }

    @PostMapping("/refresh")
    public ApiResponse<LoginResponseDTO> refreshToken(@RequestParam String refreshToken) {
        LoginResult result = authApplicationService.refreshToken(refreshToken);
        LoginResponseDTO response = AuthDTOConverter.toLoginResponseDTO(result);
        return ApiResponse.ok(response);
    }

    @GetMapping("/userinfo")
    public ApiResponse<AuthUser> getUserInfo(HttpServletRequest request) {
        String accessToken = getAccessTokenFromRequest(request);
        if (accessToken == null) {
            return ApiResponse.fail("未提供访问令牌");
        }

        AuthUser user = authApplicationService.getCurrentUser(accessToken);
        return ApiResponse.ok(user);
    }

    /**
     * 从请求中获取访问令牌
     */
    private String getAccessTokenFromRequest(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        if (authorization != null && authorization.startsWith("Bearer ")) {
            return authorization.substring(7);
        }
        return null;
    }

}
